package actor;

import view.ActorView;

public class Player extends Actor {
    private final double ATTACK_SUCCESS = 0.8;
    private final double HEAL_SUCCESS = 0.5;
    private final double POWER_SUCCESS = 0.75;

    public Player(String name, int speed, int power, int life, String weaponType, String image) {
        super(name, speed, power, life, image, ElementType.valueOf(weaponType));
    }

    @Override
    public int attack(ActorView actor) {
        Monster monster = (Monster) actor.getActor();
        double rand = Math.random();
        int power = getPower();
        ElementType weaponType = getType();
        ElementType monsterType = monster.getType();
        if ((weaponType == ElementType.ICE && monsterType == ElementType.WATER)
                || (weaponType == ElementType.FIRE && monsterType == ElementType.ICE)
                || (weaponType == ElementType.WATER && monsterType == ElementType.FIRE)) {
            power = power * 2;
        }
        if (rand < ATTACK_SUCCESS) {
            actor.beingAttack(power);
            return power;
        }
        return -1;
    }

    public int heal(ActorView player) {
        int oldHealth=getHealth();
        double rand = Math.random();
        if (rand < HEAL_SUCCESS) {
            player.beingHeal(getPower());
            return getHealth()-oldHealth;
        }
        return -1;
    }

    public boolean power(Player player) {
        if (Math.random() < POWER_SUCCESS&&getSpeed()>1) {
            player.doublePower();
            divideSpeed();
            return true;
        }
        return false;
    }

    public int addHealth(int power) {
        setHealth(getHealth() + power);
        if (getHealth() > getLife()) {
            setHealth(getLife());
        }
        return getHealth();
    }

    public void doublePower() {
        setPower(getPower() * 2);
    }

    public void divideSpeed() {
        setSpeed(getSpeed() / 2);
    }
}
